<?php
/* 
	This file is Copyright Matthew Wilkinson 2009.
	
	This file is part of Oxygen.

	Oxygen is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License version 3 as published by
	the Free Software Foundation at http://www.gnu.org/licenses/gpl-3.0.html

	Oxygen is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

*/

	Final Class O_MySQL extends DatabaseDriver
		{
		
		private $mysqliinstance = null;
		
		private $lastinsertedid = null;
		private $lastaffectedrows = null;
		
		public function __construct($connstring)
			{
			if (Security::Caller() == 'Database')
				{
				$connstring = explode('|', $connstring);
				if (isset($connstring[4]))
					{
					$this->mysqliinstance = new mysqli(	(isset($connstring[0])?$connstring[0]:ini_get("mysqli.default_host")),
										(isset($connstring[1])?$connstring[1]:ini_get("mysqli.default_user")),
										(isset($connstring[2])?$connstring[2]:ini_get("mysqli.default_pw")),
										(isset($connstring[3])?$connstring[3]:ini_get("mysqli.default_port")),
										(isset($connstring[4])?$connstring[4]:ini_get("mysqli.default_socket"))
										);
					}
				else
					{
					$this->mysqliinstance = new mysqli(	(isset($connstring[0])?$connstring[0]:ini_get("mysqli.default_host")),
										(isset($connstring[1])?$connstring[1]:ini_get("mysqli.default_user")),
										(isset($connstring[2])?$connstring[2]:ini_get("mysqli.default_pw")),
										(isset($connstring[3])?$connstring[3]:ini_get("mysqli.default_port"))
										);
					}
				if ($this->mysqliinstance->connect_error)
					{
					//throw new Exception();
					echo $this->mysqliinstance->connect_error;
					}
				}
			else
				{
				throw new Exception('database/no_direct_driver_instantiation');
				}
			}
		
		public function query($query)
			{
			$this->lastinsertid = null;
			$this->lastaffectedrows = null;
			if (Security::Caller() == 'Database')
				{
				$keyword = Array();
				preg_match('/^(.*?) /', $query, $keyword);
				$keyword = strtoupper($keyword[1]);
				/*if (!($keyword == 'SELECT' OR $keyword == 'INSERT' OR $keyword == 'DELETE' OR $keyword == 'UPDATE' OR (Oxygen\Core\CurrentUser->admin AND ($keyword == 'SHOW' OR $keyword == 'DESCRIBE' OR $keyword == 'EXPLAIN'))))
					{
					throw new Oxygen\Core\Exception(Oxygen\Core\Exceptions\O_EXT_DRIVER, Oxygen\Core\Exceptions\_DB_OP_NOT_PERMITTED);
					}
				else
					{*/
					$result = $this->mysqliinstance->query($query);
					if (!$result)
						{
						throw new Exception('database/query_failed');
						}
					elseif ($keyword == 'SELECT')
						{
						$rows = Array();
						while ($row = $result->fetch_assoc())
							{
							$rows[] = $row;
							}
						$result->free();
						return $rows;
						}
					elseif ($keyword == 'INSERT')
						{
						$this->lastinsertedid = $this->mysqli->insert_id;
						$this->lastaffectedrows = null;
						return TRUE;
						}
					elseif ($keyword == 'UPDATE' OR $keyword == 'DELETE')
						{
						$this->lastinsertedid = null;
						$this->lastaffectedrows = $this->mysqli->affected_rows;
						return TRUE;
						}
					//}
				}
			else
				{
				throw new Exception();
				}
			}
		
		public function inserted_id()
			{
			if (Security::Caller() == 'Database')
				{
				return $this->lastinsertedid;
				}
			else
				{
				throw new BadMethodCall('database/op_not_permitted');
				}
			}
		
		public function affected_rows()
			{
			if (Security::Caller() == 'Database')
				{
				return $this->lastaffectedrows;
				}
			else
				{
				throw new BadMethodCall('database/op_not_permitted');
				}
			}
		
		}

?>